
/*
 * @Author: your name
 * @Date: 2021-03-19 20:01:15
 * @LastEditTime: 2021-04-21 19:04:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /dvr-ui/src/methods/base.js
 */

import axios from 'axios'//引用axios



/**
 * @description: 
 * @param {*} key
 * @return {*}
 */
let sumHandleSelect = function (key) {
  if (key == "1") {
    this.$router.push({ path: '/' });
  }
  if (key == "2") {
    this.$router.push({ name: 'upload', path: '/upload' })
  }
}


/**
 * @description:  实现动态生成html卡片
 * @param {*} farry 文件数组
 * @param {*} id 标签id
 * @param {*} cno 返回页号
 * @return {*}
 */
let createCards = function (farry, id = "cards", cno = 0) {
  // //TODO:实现动态生成卡片
  let father = document.getElementById(id)
  if (father.hasChildNodes) {
    father.parentNode.removeChild(father)
  }
  // 当页面切换时，也调用该方法
  //TODO : 实现页面切换功能
  console.log(cno)
  let row = Math.ceil(farry.length / 3)
  let z = 0;//遍历给的元素
  for (let i = 0; i < row; i++) {
    let mrow = document.createElement("ul")
    for (let j = 0; j < 3 || z < farry.length; j++, z++) {
      let md = document.createElement("li")
      md.style.width = "25%"
      let mcard = document.createElement("div")
      mcard.classList.add("el-card")
      mcard.shadow = "hover"
      // TODO :insert information
      md.appendChild(mcard)
      mrow.appendChild(md)
    }
  }
}

/**
 * @description: 实现搜索功能
 * @param {str} str 搜索的内容
 * @param {int} value 当前搜索类型，1为视频标题,2为视频文件名
 * @return null
 */
let search = function (str, value) {
  console.log(value)
  let _this = this
  if (str == '' || str == undefined || str == null) {
    this.$message.warning(`"请在搜索框输入内容后再搜索"`);
  } else {
    //console.log('str:' + str + '，value:' + value)
    axios({
      headers: {
        'Content-type': 'charset=UTF-8'
      },
      method: 'post',
      url: 'http://localhost:8000/video/search',
      data: { 'str': str, 'value': value },

    }).then(function (response) {
      //console.log(response.data.data)
      let rdata = response.data.data
      _this.mvTotal = rdata.length
      _this.fileList = rdata
      _this.sName = str
      _this.sTag = value
      //console.log(rdata)
      // createCards(rdata)
    })
    // .catch(function () {
    //   _this.$notify.error({
    //     title: '错误',
    //     message: '访问数据库失败，应该是服务器问题'
    //   })
    //   ;
    // })
  }
}

/**
 * @description: 重置表单所有信息
 * @param {*}
 * @return {*}
 */
let resetForm = function (formName) {
  this.$refs[formName].resetFields();
}


export { sumHandleSelect, search, resetForm, createCards }